/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrsudoku;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.text.PlainDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;

 /**
         *  PW
         *  hier wird die eingabe auf ein feld limitiert und die eingabe auf
         *  1-9 begrenzt
         */
public class MrSudokuDigitBox extends JTextField
{

    private int x;
    private int y; // positions in th 9x9 sudokumatrix

    public MrSudokuDigitBox(int i, int j)
    {

        if (x < 0 || x > 9)
        {
            throw new IllegalArgumentException("x must be an integer between 0 and 9.");
        } else if (y < 0 || y > 9)
        {
            throw new IllegalArgumentException("y must be an integer between 0 and 9.");
        } else
        {
            this.x = i;
            this.y = j;
        }




        //optionen für boxen PW
        Font fonty = new Font("Leelawadee", Font.PLAIN, 30);
        this.setBorder(new LineBorder(Color.GRAY, 2, true));
        this.setHorizontalAlignment(JTextField.CENTER);
        this.setFont(fonty);
        this.setForeground(Color.DARK_GRAY);

        
        
        this.setDocument(new PlainDocument()
        {

            private static final int LIMIT = 1;

            @Override
            public void insertString(int offset, String str, AttributeSet attr)
                    throws BadLocationException
            {
                if (((getLength() + str.length()) <= LIMIT) && (str.matches("^[1-9]$")))   // nur noch eingabe von 1-9 möglich und 1 char begrenzung
                {
                    super.insertString(offset, str, attr);
                    // System.out.println(MrSudokuApp.getApplication().getActiveFile());

                    
                    
                    MrSudokuApp.getApplication().setArrayElement(x, y, getNumber());


                }





            }
        });


        this.addMouseWheelListener(new MouseWheelListener()
        {

            public void mouseWheelMoved(MouseWheelEvent e)
            {

                MrSudokuDigitBox box = (MrSudokuDigitBox) e.getSource();
                if (box.isEditable() && box.isEnabled())
                {

                    // mousewheel hoch soll zahlen inkrementieren und vice versa

                    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
                    {

                        int inkr = getNumber() + e.getWheelRotation();

                        if (inkr < 0)
                        {
                            inkr = 9;
                        }

                        setText(Integer.toString(inkr));


                    }
                }


            }
        });


        this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e)
            {
                 
            }

            public void mousePressed(MouseEvent e)
            {
                
            }

            public void mouseReleased(MouseEvent e)
            {
            }

            public void mouseEntered(MouseEvent e)
            {
                //System.out.println(x + " , "+ y);
                setBackground(Color.white);
                setForeground(Color.DARK_GRAY);
            }

            public void mouseExited(MouseEvent e)
            {
            }
        });
        
        this.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
               
            }
        });
        

    }

    public int[] getPosition()
    {
        int[] pos = new int[2];
        pos[0] = x;
        pos[1] = y;
        return pos;
    }

    public int getNumber()
    {

        String txt = this.getText();
        int number;

        // bei keiner angabe werden die werte vorläufig als 0 gespeichert, jedoch wird 0 in der gui als leeres feld angezeigt
        if (txt.equals(""))
        {
            number = 0;
        } else
        {
            try
            {
                number = Integer.parseInt(txt);
            } catch (NumberFormatException nfe)
            {
                //todo log
               // nfe.printStackTrace();
                number = 0;
            }

        }

        return number;
    }

    void setNumber(int i)
    {
        String digit;

        if (i == 0)
        {
            digit = "";
        } else
        {
            digit = Integer.toString(i);
        }

        this.setText(digit);
        
    }
}
